package com.xinhua.web.servlet;

import com.xinhua.util.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

//修改
@WebServlet("/UpdateTowServlet")
public class UpdateTowServlet extends HttpServlet {
    private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String price = req.getParameter("price");
        String type = req.getParameter("type");
        String time = req.getParameter("time");
        String sql = "update day23.consumption set name=?,price=?,type=?,time=? where id=? ";
        try {
            queryRunner.update(sql, name, price, type, time, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        resp.sendRedirect(req.getContextPath() + "/findServlet");
    }
}
